// 岛屿数量

import Foundation

var gridMian:[[Character]] = [[]]

func numIslands(_ grid: [[Character]]) -> Int {
    if grid.count == 0 {
        return 0;
    }
    if grid[0].count == 0 {
        return 0;
    }
    
    gridMian = grid;
    var result = 0;
    
    for i in 0..<grid.count {
        for j in 0..<grid[0].count {
            if(gridMian[i][j] == "1") {
                result+=1;
                checkLands( i, j);
            }
        }
    }
    
    return result;
}

func checkLands(_ i:Int,_ j:Int) {
    
    if i<0 || i >= gridMian.count || j<0 || j >= gridMian[0].count || gridMian[i][j] == "0" {
        return;
    }
    gridMian[i][j] = "0";
    checkLands( i+1, j);
    checkLands( i, j+1);
    checkLands( i, j-1);
    checkLands( i-1, j);
}

